<link rel="stylesheet" type="text/css" href="<?php echo base_url() . 'public/css/dateinput.css'; ?>"/>

<script>
    $(document).ready(function () {
        $.tools.dateinput.localize("et", {
            months: 'Jaanuar,Veebruar,Märts,Aprill,Mai,Juuni,Juuli,August,September,Oktoober,November,Detsember',
            shortMonths:  'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec',
            days:         'Pühapäev,Esmaspäev,Teisipäev,Kolmapäev,Neljapäev,Reede,Laupäev',
            shortDays:    'P,E,T,K,N,R,L'
        });
  
        $(":date").dateinput({
            trigger: true,
            lang: 'et',
            firstDay: 1,
            // this is displayed to the user
            format: 'dd.mm.yyyy'
        });
        
        $(":date").bind("onShow onHide", function()  {
            $(this).parent().toggleClass("active");
        });

        // a different format is sent to the server
        $(":date:first").data("dateinput").change(function() {
            var isoDate = this.getValue('yyyy-mm-dd');
            $("#start_date").val(isoDate);
        });
        // a different format is sent to the server
        $(":date:last").data("dateinput").change(function() {
            var isoDate = this.getValue('yyyy-mm-dd');
            $("#end_date").val(isoDate);
        });
        
        $( "ul.droptrue" ).sortable({
            connectWith: "ul",
            items: "li:not(.disabled)"
        });
 
        $( "ul.dropfalse" ).sortable({
            connectWith: "ul",
            dropOnEmpty: false
        });
 
        $( ".sortable-event" ).disableSelection();
        
        $('#mdb').submit(function() {
            $('#events').val($('#found-events').html());
        });
    });

</script>

<style>
    .sortable-event { list-style-type: none; margin: 5px; padding: 0; float: left; margin-right: 10px; background: #eee; padding: 5px; min-height: 100px; }
    .sortable-event li { margin: 5px; padding: 5px; font-size: 0.9em; }
    .ui-state-highlight {cursor: pointer; }
    .ui-state-default {font-weight: bold; }
</style>

<?php
echo form_open('files/add_mdb', array('id' => 'mdb'));
echo form_input(array('name' => 'events', 'type' => 'hidden', 'id' => 'events'));
echo form_hidden('full_path', $full_path);
?>

<h2>Leitud võistlus <br /> <?php echo $competition->getName(); ?> </h2>
<ul>
    <?php
    echo '<li>Võistluse nimi: ' . form_input('name', $competition->getName()) . '</li>';
    echo '<li>Riik: ' . $country_input . '</li>';
    echo '<li>Linn: ' . form_input('city', '') . '</li>';
    echo '<li>Ujula: ' . form_input('facility', $competition->getFacility()) . '</li>';
    echo '<li>Bassein: ' . form_dropdown('pool', array(25 => '25m', 50 => '50m'), "{$competition->getPool()}") . '</li>';
    $data_start = array(
        'type' => 'date',
        'value' => $competition->getStartDate(),
    );
    echo '<li>Alguskuupäev*: ' . form_input($data_start) . '</li>';
    $data_end = array(
        'type' => 'date',
        'value' => $competition->getEndDate(),
    );
    echo '<li>Lõppkuupäev*: ' . form_input($data_end) . '</li>';
    $data_start_hidden = array(
        'type' => 'hidden',
        'id' => 'start_date',
        'name' => 'start_date',
        'value' => $competition->getStartDate(),
    );
    echo form_input($data_start_hidden);
    $data_end_hidden = array(
        'type' => 'hidden',
        'id' => 'end_date',
        'name' => 'end_date',
        'value' => $competition->getEndDate(),
    );
    echo form_input($data_end_hidden);
    echo form_submit('add_mdb', 'Lisa tulemused');
    echo form_close();
    ?>
</ul>

<h2>Kokku leiti <?php echo $count_events ?> ala. </h2>
<h2>Kokku leiti <?php echo $count_results ?> tulemust. </h2>


<div id="found-events">
    <?php
    $events_per_day = ceil($count_events / ($days + 1));
    $events_added_total = 0;
    for ($day = 0; $day <= $days; $day++):
        ?>
        <ul class="droptrue sortable-event" id="day<?php echo $day + 1; ?>">
            <?php $datestring = ($day + 1) . '. päev - ' . date('d.m.Y', strtotime($start_date) + $day * 86400); ?>
            <li class="ui-state-default disabled"><?php echo $datestring; ?></li> 
            <?php
            for ($i = $events_added_total, $events_added = 0; $events_added < $events_per_day && $i < $count_events; $i++) :
                $event = $events[$i];
                $events_added_total++;
                $events_added++;
                ?>
                <li class="ui-state-highlight"><?php echo $event->getRaw(); ?></li>
            <?php endfor; ?>
        </ul>
    <?php endfor; ?>

    <br style="clear: both;" />
</div>
